<?php

namespace Hilaoyu\LaravelExtend\Services\Captcha\Controllers;

use Hilaoyu\LaravelExtend\ControllerBase;
use Hilaoyu\LaravelExtend\Services\Captcha\CaptchaService;
use Illuminate\Http\Request;

class CaptchaController extends ControllerBase
{

    public function image(Request $request){
        $width = $request->input('w',150);
        $height = $request->input('h',40);

        $entity = new CaptchaService();

        $captcha = $entity->generateImg($width,$height);

        if(empty($captcha)){
            comResponse()->failed('生成验证码失败')->withErrors($entity->getError());
        }else{
            $data = [
                'code_id'=>$captcha['code_id'],
                'code_img'=>$captcha['code'],
            ];
            comResponse()->success('生成验证码成功')->with($data);
        }

        return comResponse()->json();
    }

    public function email(Request $request){
        $email = $request->input('email','');
        $code_img_id = $request->input('code_img_id','');
        $code_img_val = $request->input('code_img_val','');
        $template = $request->input('tpl','captcha');

        $entity = new CaptchaService();
        if(!$entity->verify($code_img_id,$code_img_val,null,true)){
            return comResponse()->failed('图形验证码错误')->withErrors('图形验证码错误','code_img_val')->json();
        }

        $code_id = $entity->generateEmail($email,$template);

        if(empty($code_id)){
            comResponse()->failed('发送验证码失败')->withErrors($entity->getError(false),'email');
        }else{
            $data = [
                'code_id'=>$code_id,
            ];
            comResponse()->success('发送验证码成功')->with($data);
        }

        return comResponse()->json();
    }

    public function mobile(Request $request){
        $phone = $request->input('mobile','');
        $code_img_id = $request->input('code_img_id','');
        $code_img_val = $request->input('code_img_val','');
        $template = $request->input('tpl','captcha');

        $entity = new CaptchaService();
        if(!$entity->verify($code_img_id,$code_img_val,null,true)){
            return comResponse()->failed('图形验证码错误')->withErrors('图形验证码错误','code_img_val')->json();
        }

        $code_id = $entity->generateMobile($phone,$template);

        if(empty($code_id)){
            comResponse()->failed('发送验证码失败')->withErrors($entity->getError(false),'mobile');
        }else{
            $data = [
                'code_id'=>$code_id,
            ];
            comResponse()->success('发送验证码成功')->with($data);
        }

        return comResponse()->json();
    }



    public function check(Request $request){
        $code_id = $request->input('code_id','');
        $code_val = $request->input('code_val','');
        $send_to = $request->input('send_to','');

        $entity = new CaptchaService();
        if($entity->verify($code_id,$code_val,$send_to)){
            return comResponse()->success('验证码正确')->json();
        }
        return comResponse()->failed('验证码错误')->json();

    }



}
